package com.kefu.chat.controller;


import com.kefu.chat.model.*;
import com.kefu.chat.service.ChatAdminService;
import com.kefu.chat.service.ChatConversationService;
import com.kefu.chat.service.ChatRobotService;
import com.kefu.chat.service.ChatUserService;
import com.kefu.chat.util.ChatConstant;
import com.kefu.chat.vo.*;
import com.kefu.chat.vo.ChatAdminParam.*;
import com.kefu.chat.vo.ChatUserParam.UserGetSessionMsgP;
import com.kefu.chat.vo.ChatUserParam.UserGetSessionMsgR;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DetailVo;
import com.kefu.crm.event.CrmCustomerEvent;
import com.kefu.crm.event.CrmCustomerLogEvent;
import com.kefu.crm.model.CrmCustomer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "在线客服接口")
@RestController
@RequestMapping("chat/agent")
public class ChatAdminController extends AbstractBaseController {
    @Autowired
    private ChatAdminService chatAdminService;
    @Autowired
    private ChatUserService chatUserService;
    @Autowired
    private ChatRobotService chatRobotService;
    @Autowired
    private ChatConversationService chatConversationService;

    @PostMapping("/info")
    @ApiOperation(value = "客服信息")
    public Result<ChatAdmin> info() {
        return ResultGenerator.genSuccessResult(chatAdminService.info());
    }

    @PostMapping("/online")
    @ApiOperation(value = "客服上线")
    public Result<ChatAdmin> online() {
        logger.info("客服上线:{}", KefuContext.getUid());
        return ResultGenerator.genSuccessResult(chatAdminService.online());
    }

    @PostMapping("/busy")
    @ApiOperation(value = "客服忙碌")
    public Result<ChatAdmin> busy() {
        logger.info("客服忙碌:{}", KefuContext.getUid());
        return ResultGenerator.genSuccessResult(chatAdminService.busy(ChatConstant.admin_status_busy));
    }

    @PostMapping("/out")
    @ApiOperation(value = "客服离线")
    public Result<String> out() {
        logger.info("客服离线:{}", KefuContext.getUid());
        chatAdminService.out(KefuContext.getKefuContext().getUserId(), ChatConstant.out_type_close);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/sendMsg")
    @ApiOperation(value = "给用户发消息")
    public Result<AdminSendMsgR> sendMsg(@Validated @RequestBody AdminSendMsgP param) {
        return ResultGenerator.genSuccessResult(chatAdminService.sendMsg(param));
    }

    @PostMapping("/getEventMsg")
    @ApiOperation(value = "轮询事件消息")
    public Result<AdminGetEventMsgR> getEventMsg(@Validated @RequestBody AdminGetEventMsgP param) {
        return ResultGenerator.genSuccessResult(chatAdminService.getEventMsg(param));
    }

    @PostMapping("/removeUser")
    @ApiOperation(value = "结束用户会话")
    public Result<String> removeUser(@Validated @RequestBody AdminUserP param) {
        chatAdminService.removeUser(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/transferUser")
    @ApiOperation(value = "转接用户会话")
    public Result<String> transferUser(@Validated @RequestBody AdminTransferUserP param) {
        chatAdminService.transferUser(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/inviteUser")
    @ApiOperation(value = "邀请用户")
    public Result<String> inviteUser(@Validated @RequestBody AdminUserP param) {
        chatAdminService.inviteUser(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/getSessionMsg")
    @ApiOperation(value = "查询单个会话消息")
    public Result<List<UserGetSessionMsgR>> getSessionMsg(@Validated @RequestBody UserGetSessionMsgP param) {
        return ResultGenerator.genSuccessResult(chatUserService.getSessionMsg(param));
    }

    @PostMapping("/getHistoryMsg")
    @ApiOperation(value = "查询历史会话消息")
    public Result<List<UserGetSessionMsgR>> getHistoryMsg(@Validated @RequestBody ChatAdminParam.AdminGetHistoryMsgP param) {
        return ResultGenerator.genSuccessResult(chatAdminService.getHistoryMsg(param));
    }

    @PostMapping("/getOnlineUser")
    @ApiOperation(value = "查询在线用户列表")
    public Result<List<AdminOnlineUserR>> getOnlineUser() {
        return ResultGenerator.genSuccessResult(chatAdminService.getOnlineUser());
    }

    @PostMapping("/getWaitUser")
    @ApiOperation(value = "查询排队用户列表")
    public Result<List<AdminWaitUserR>> getWaitUser() {
        return ResultGenerator.genSuccessResult(chatAdminService.getWaitUser());
    }

    @PostMapping("/getHistorySession")
    @ApiOperation(value = "查询历史会话列表")
    public Result<List<ChatSession>> getHistorySession(@Validated @RequestBody AdminGetHistorySessionP param) {
        return ResultGenerator.genSuccessResult(chatAdminService.getHistorySession(param));
    }

    @PostMapping("/getOtherAdmin")
    @ApiOperation(value = "查询其他客服列表")
    public Result<List<ChatAdmin>> getOtherAdmin() {
        return ResultGenerator.genSuccessResult(chatAdminService.getOtherAdmin());
    }

    @PostMapping("/getUserInfo")
    @ApiOperation(value = "查询用户信息")
    public Result<ChatUser> getUserInfo(@Validated @RequestBody AdminUserP param) {
        return ResultGenerator.genSuccessResult(chatAdminService.getUserInfo(param));
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation(value = "修改用户信息")
    public Result<String> updateUserInfo(@Validated @RequestBody AdminUpdateUserP param) {
        chatAdminService.updateUserInfo(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/markUser")
    @ApiOperation(value = "会话星标")
    public Result<String> markUser(@Validated @RequestBody AdminMarkUserP param) {
        chatAdminService.markUser(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/blackUser")
    @ApiOperation(value = "用户拉黑")
    public Result<String> blackUser(@Validated @RequestBody AdminBlackUserP param) {
        CrmCustomer crmCustomer = chatAdminService.blackUser(param);
        publisher.publishEvent(new CrmCustomerLogEvent(crmCustomer, 0 == param.getBlackMode()  ? 5 : 4));
        publisher.publishEvent(new CrmCustomerEvent(crmCustomer));
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/inviteEvaluate")
    @ApiOperation(value = "邀请用户评价")
    public Result<String> inviteEvaluate(@Validated @RequestBody AdminUserP param) {
        chatAdminService.inviteEvaluate(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/updateSummary")
    @ApiOperation(value = "修改会话总结")
    public Result<String> updateSummary(@Validated @RequestBody AdminSummary param) {
        chatAdminService.updateSummary(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/typing")
    @ApiOperation(value = "正在输入")
    public Result<String> typing(@Validated @RequestBody AdminUserP param) {
        chatAdminService.typing(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/getReplyList")
    @ApiOperation(value = "查询快捷回复")
    public Result<List<?>> getReplyList(@Validated @RequestBody AdminGetReplyListP param) {
        List<?> replyList = chatAdminService.getReplyList(param);
        return ResultGenerator.genSuccessResult(replyList);
    }

    @PostMapping("/robotAsk")
    @ApiOperation(value = "机器人智能回复")
    public Result<AdminRobotAskR> robotAsk(@Validated @RequestBody AdminRobotAskP param) {
        return ResultGenerator.genSuccessResult(chatRobotService.adminRobotAsk(param));
    }

    @PostMapping("/recallMsg")
    @ApiOperation(value = "消息撤回")
    public Result<String> recallMsg(@Validated @RequestBody AdminRecallMsgP param) {
        chatAdminService.recallMsg(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/grabUser")
    @ApiOperation(value = "抢接用户会话")
    public Result<String> grabUser(@Validated @RequestBody AdminUserP param) {
        chatAdminService.grabUser(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/getReceptConfig")
    @ApiOperation(value = "客服接待设置查询")
    public Result<ChatReceptConfig> getReceptConfig() {
        return ResultGenerator.genSuccessResult(chatAdminService.getReceptConfig());
    }

    @PostMapping("/updateReceptConfig")
    @ApiOperation(value = "客服接待设置修改")
    public Result<String> updateReceptConfig(@Validated @RequestBody ChatReceptConfig param) {
        chatAdminService.updateReceptConfig(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/updateWoCreateMode")
    @ApiOperation(value = "更新创建工单标识")
    public Result<String> updateWoCreateMode(@Validated @RequestBody AdminUserP param) {
        chatAdminService.updateWoCreateMode(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/getAllGroupAdmin")
    @ApiOperation(value = "查询所有客服列表")
    public Result<List<GetAllGroupAdminVo>> getAllGroupAdmin() {
        return ResultGenerator.genSuccessResult(chatConversationService.getAllGroupAdmin());
    }

    @PostMapping("/conversationAdd")
    @ApiOperation(value = "内部会话添加",notes = "返回会话id")
    public Result<String> conversationAdd(@RequestBody ChatConversationInsertVo param) {
        return ResultGenerator.genSuccessResult(chatConversationService.conversationAdd(param));
    }

    @PostMapping("/conversationList")
    @ApiOperation(value = "内部会话列表")
    public Result<List<ChatConversation>> conversationList(@RequestBody ChatConversationSearchVo param) {
        return ResultGenerator.genSuccessResult(chatConversationService.conversationList(param));
    }

    @PostMapping("/conversationDetail")
    @ApiOperation(value = "内部会话详情",notes = "比list接口，多返回userList会话中所有客服信息")
    public Result<ChatConversationDetailVo> conversationDetail(@RequestBody DetailVo detailVo) {
        return ResultGenerator.genSuccessResult(chatConversationService.conversationDetail(detailVo.getId()));
    }

    @PostMapping("/conversationSendMsg")
    @ApiOperation(value = "内部会话发送消息")
    public Result<ChatConversationMsgSendResult> conversationSendMsg(@RequestBody ChatConversationMsgSendVo param) {
        return ResultGenerator.genSuccessResult(chatConversationService.conversationSendMsg(param));
    }

    @PostMapping("/conversationHistoryMsg")
    @ApiOperation(value = "内部会话历史会话消息")
    public Result<List<ChatConversationMsg>> conversationHistoryMsg(@Validated @RequestBody ConversationHistoryMsgQuery param) {
        return ResultGenerator.genSuccessResult(chatConversationService.conversationHistoryMsg(param));
    }

    @PostMapping("/conversationRecallMsg")
    @ApiOperation(value = "内部会话消息撤回")
    public Result<String> conversationRecallMsg(@Validated @RequestBody ConversationRecallMsgParam param) {
        chatConversationService.conversationRecallMsg(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/getVideoToken")
    @ApiOperation(value = "客服请求视频token")
    public Result<GetTokenResult> getVideoToken() {
        GetTokenResult result = chatAdminService.getAdminVideoToken();
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/expireVideoToken")
    @ApiOperation(value = "删除客服视频token")
    public Result<Boolean> expireVideoToken(@Validated @RequestBody ChatRongYunExpireTokenVo vo) {
        Boolean result = chatAdminService.rongYunExpireToken(vo);
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/adminAcceptVideoInvitation")
    @ApiOperation(value = "客服接受音视频通话")
    public Result<String> adminAcceptVideoInvitation(@Validated @RequestBody AdminUserP param) {
        chatAdminService.acceptVideo(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/adminRefusedVideoInvitation")
    @ApiOperation(value = "客服拒绝音视频通话")
    public Result<String> adminRejectCall(@Validated @RequestBody AdminUserP param) {
        chatAdminService.refuseVideo(param);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/sysMsg")
    @ApiOperation(value = "系统消息")
    public Result<String> sysMsg(@Validated @RequestBody ChatAdminParam.AdminSysMsgP param) {
        String msgId = chatAdminService.sysMsg(param);
        return ResultGenerator.genSuccessResult(msgId);
    }

    @PostMapping("/change")
    @ApiOperation(value = "状态变更")
    public Result<ChatAdmin> change(@Validated @RequestBody AdminStatusChangeP change) {
        logger.info("状态变更:{}", KefuContext.getUid());
        ChatAdmin admin = chatAdminService.change(change);
        return ResultGenerator.genSuccessResult(admin);
    }

    @PostMapping("/offline")
    @ApiOperation(value = "强制下线")
    public Result<ChatAdmin> offline(@Validated @RequestBody AdminOfflineP change) {
        logger.info("强制下线:{}", KefuContext.getUid());
        ChatAdmin admin = chatAdminService.out(change.getAdminId(), ChatConstant.out_type_force_offline);
        return ResultGenerator.genSuccessResult(admin);
    }
}
